<script type="text/javascript">
$(function(){
	var pg = App.soagov.svcTraceIndex.ui, pp = pg.params || {},
		svcId = pp.id, gd = pg.find('.grid'), envs = Cache.base.env('deploy'),
		hd = pg.children('.head'), envCb = hd.seek('env');

	envCb.mac('combo', {
		width: 70,
		boxWidth: 71,
		readOnly: 1,
		keyField: 'k',
		displayField: 'v',
		data: envs,
		onSelect: function(me, sk, sd){
			gd.load({ env: sd[0] });
		}
	}).val('');
	
	gd.mac('grid', {
		cols : [{
			field: 'id', width: 320, title: 'ID',
			render : function(r) {
				r.app = pp.app;
				return $('<span class="a"></span>').click(function(){
					var p = pg.parent();
					App.show({
						ctx: 'soagov',
						action: 'svcTraceView',
						params: r
					}, function(){
						App.show({
							ctx: 'soagov',
							action: 'svcTraceIndex',
							params: pp
						}, $.noop, p);
					}, p);
				}).append(r.id);
			}
		},{
			field: 'env', title: '所属环境', width: 80
		},{
			field: 'addr', title: '应用服务器地址', width: 160,
			render: function(r) {
				return $('<span class="a"></span>').click(function(){
					App.open({
						ctx: 'base',
						action: 'serverView',
						params: { addr: r.addr }
					});
				}).append(r.addr);
			}
		},{
			field: 'time', title: '总耗时', width: 80,
			render: function(r) {
				return (r.endTime - r.startTime) + 'ms';
			}
		},{
			field: 'startTime', title: '开始时间', width: 200,
			render: function(r) {
				return new Date(r.startTime).format('yyyy-MM-dd HH:mm:ss.S');
			}
		},{
			field: 'endTime', title: '结束时间', width: 200,
			render: function(r) {
				return new Date(r.endTime).format('yyyy-MM-dd HH:mm:ss.S');
			}
		},{
			field: 'logs', title: '日志', width: 80,
			render: function(r) {
				return $('<span class="a"></span>').click(function(){
					// TODO
					var url = '/log#query?target=' + 'logs' + '&trace=' + r.id;
					window.open(encodeURI(url));
				}).append('查看');
			}
		}],
		loader: {
			url: App.soagov.ctx + '/svc/trace/list.do' ,
			params: {
				pageNo: 1, pageSize: 20, id: pp.id,
				startTime: pp.startTime, endTime: pp.endTime
			},
			autoLoad: true
		},
		pagerLength: 20
	});
	
	pg.seek('refresh').click(function(){
		gd.load();
	});
	pg.seek('query').click(function(){
		App.open({
			ctx: 'soagov',
			action: 'svcTraceQuery',
			params: { env: envCb.val() }
		}, function(o){
			gd.load(o);
		});
	});

	function adjust(){
		var p = pg.parent(), h = p.height();
		gd.parent().height(h - 64);
		gd.adjust();
	}
	$(window).resize(adjust);
	adjust(); 
});
</script>
<div class="head">
	<div class="fl">
		<span class="fl">环境：</span>
		<div class="combo fl" name="env"></div>
	</div>
	<span>&nbsp;&nbsp;</span>
	<button name="refresh">刷新</button>
	<button name="query">自定义查询</button>
	<span>&nbsp;&nbsp;</span>
</div>
<div class="clear"></div>
<div class="body">
	<div class="grid"></div>
</div>
